

import 'package:trade/common/config/td_config.dart';
import 'package:trade/common/service/i_service.dart';
import 'package:trade/common/service/router_service/router_table.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'router_factory.dart';

/// 路由服务,项目跳转务必用此服务
class RouterService extends NavigatorObserver implements IService  {

  late BuildContext overlayContext;
  ///
  BuildContext get context => navigator!.context;
  /// 路由表
  Map<String, BaseRoute> routeTable = {};

  /// 当前路由名称
  String? currentRouteName;

  @optionalTypeArgs
  Future<T?> push<T extends Object>(Route<T> route){
    return navigator!.push(route);
  }

  @optionalTypeArgs
  Future<T?> pushAndRemoveUntil<T extends Object?>(Route<T> newRoute, RoutePredicate predicate){
    return navigator!.pushAndRemoveUntil(newRoute, predicate);
  }

  @optionalTypeArgs
  Future<T?>? pushNamed<T extends Object>(String routeName, {Object? arguments}) {
    // 防止重复入栈
    // if (currentRouteName == routeName) {
    //   return null;
    // }
    return navigator!.pushNamed(routeName, arguments: arguments);
  }

  @optionalTypeArgs
  Future<T?>? pushNamedAndRemoveUntil<T extends Object?>(
      String routeName,
      RoutePredicate predicate,
      { Object? arguments}
      ){
    //
    // if (currentRouteName == routeName) {
    //   return null;
    // }
    return navigator!.pushNamedAndRemoveUntil(routeName, predicate, arguments: arguments);
  }

  @optionalTypeArgs
  Future<T?> pushReplacement<T extends Object?, TO extends Object?>(Route<T> newRoute, {TO? result}){
    return navigator!.pushReplacement(newRoute, result: result);
  }

  @optionalTypeArgs
  Future<T?> pushReplacementNamed<T extends Object?, TO extends Object?>(
      String routeName,
      {TO? result,
        Object? arguments}) {
    return navigator!.pushReplacementNamed(routeName, result: result, arguments: arguments);
  }

  @optionalTypeArgs
  void pop<T extends Object?>([T? result]){
    navigator!.pop(result);
  }

  @optionalTypeArgs
  Future<T?> popAndPushNamed<T extends Object?, TO extends Object?>(
      String routeName,
      {TO? result,
        Object? arguments}) {
    return navigator!.popAndPushNamed(routeName, arguments: arguments, result: result);
  }

  void popUntil(RoutePredicate predicate){
    navigator!.popUntil(predicate);
  }

  void removeRoute(Route<dynamic> route){
    navigator!.removeRoute(route);
  }

  bool canPop(){
    return navigator!.canPop();
  }

  @optionalTypeArgs
  Future<bool> maybePop<T extends Object?>([T? result]) async {
    return navigator!.maybePop(result);
  }


  @override
  void didPush(Route route, Route? previousRoute) {
    currentRouteName = route.settings.name;
    super.didPush(route, previousRoute);
  }

  @override
  void didPop(Route route, Route? previousRoute) {
    currentRouteName = previousRoute?.settings.name;
    super.didPop(route, previousRoute);
  }
  
  @override
  Future<void>? init() {
    routeTable.addAll(RouterTable.table);
    return null;
  }

  Route<dynamic>? generateRoute(RouteSettings settings) {
    List<String> matchKeys =
    routeTable.keys.where((element) => (element == settings.name)).toList();
    String? matchKey = matchKeys.length > 0 ? matchKeys.first : null;
    if (null != matchKey) {
      BaseRoute? baseRoute = routeTable[matchKey];
      if(null != baseRoute){
        return baseRoute.getRoute(settings);
      }
    }
    return null;
  }
}
